package com.columboid.protocol.syncml.tests;

import java.security.NoSuchAlgorithmException;

import junit.framework.TestCase;

import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.columboid.protocol.syncml.Credential;
import com.columboid.protocol.syncml.CredentialType;
import com.columboid.protocol.syncml.ServerAuthentication;
import com.columboid.protocol.syncml.tests.mock.MockCredential;

@RunWith(JMock.class)
public class AuthenticationTest extends TestCase {

	Mockery context = new JUnit4Mockery();

	@Override
	@Before
	public void setUp() {

	}

	@Test
	public void testProperPasswordAndUserNameMatch()
			throws NoSuchAlgorithmException {

		Boolean result = true;
		final Credential credential = context.mock(Credential.class);
		ServerAuthentication authentication = new ServerAuthentication();
		MockCredential.mockValidUserNameAndPassword(context, credential);
		result = authentication.checkCredential(credential, MockCredential
				.mockValidHashPasswordValue());
		assertTrue(result == true);

	}

	@Test
	public void testInvalidPasswordAndUserNameNotMatch() {

	}

	@Test
	public void testHashPasswordAndNonceMatch() throws NoSuchAlgorithmException {
		Boolean result = true;
		final Credential credential = context.mock(Credential.class);
		ServerAuthentication authentication = new ServerAuthentication();
		MockCredential.mockValidUserCredential(context, credential);
		result = authentication.checkCredential(credential, MockCredential
				.mockValidNonceHashValue());
		assertTrue(result == true);

	}

	@Test
	public void testInvalidHashPasswordAndNonceNotMacth()
			throws NoSuchAlgorithmException {
		Boolean result = true;
		final Credential credential = context.mock(Credential.class);
		ServerAuthentication authentication = new ServerAuthentication();
		MockCredential.mockInValidUserCredential(context, credential);
		result = authentication.checkCredential(credential, MockCredential
				.mockValidNonceHashValue());
		assertTrue(result == false);

	}

	@Test
	public void testMD5Authentication() throws NoSuchAlgorithmException {

		Boolean result = true;
		final Credential credential = context.mock(Credential.class);
		ServerAuthentication authentication = new ServerAuthentication();
		authentication.setCredentialType(CredentialType.MD5);
		MockCredential.mockValidUserCredential(context, credential);
		result = authentication.checkCredential(credential, MockCredential
				.mockValidNonceHashValue());
		assertTrue(result == true);
	}

	@Test
	public void testSHAAuthentication() throws NoSuchAlgorithmException {

		Boolean result = true;
		final Credential credential = context.mock(Credential.class);
		ServerAuthentication authentication = new ServerAuthentication();
		authentication.setCredentialType(CredentialType.SHA);
		MockCredential.mockValidSHAUserCredential(context, credential);
		result = authentication.checkCredential(credential, MockCredential
				.mockValidSHANonceHashValue());
		assertTrue(result == true);
	}

}
